/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-08-03     liuchao       the first version
 */
#define LOG_TAG "led_beep"
#include <ulog.h>
#include "led_beep.h"
#include <rtthread.h>
#include <signal_led.h>
#include <board.h>

#define SYS_LED GET_PIN(D, 11)

static led_t* s_sys_led = NULL;
static rt_base_t* s_sys_led_pin = NULL;

static led_t* s_panel_yellow = NULL;
static rt_base_t* s_panel_yellow_pin = NULL;

static led_t* s_panel_green = NULL;
static rt_base_t* s_panel_green_pin = NULL;

static led_t* s_panel_red = NULL;
static rt_base_t* s_panel_red_pin = NULL;

static led_t* s_beep = NULL;
static rt_base_t* s_beep_pin = NULL;

static led_notify_mode_e s_cur_state[LED_BEEP_END];

static void net_down_over_callback(led_t *led_handler)
{
    if (led_handler == s_panel_red)
    {
        s_cur_state[PANEL_LED_RED] = LED_NOTIFY_MODE_OFF;
    }
    else if (led_handler == s_beep)
    {
        s_cur_state[PANEL_BEE] = LED_NOTIFY_MODE_OFF;
    }
}

void led_beep(led_beep_e led_beep, led_notify_mode_e mode)
{
    led_t* led_handler = 0;
    switch (led_beep)
    {
    case PANEL_BEE:
        led_handler = s_beep;
        break;
    case PANEL_LED_YELLOW:
        led_handler = s_panel_yellow;
        break;
    case PANEL_LED_RED:
        led_handler = s_panel_red;
        break;
    case PANEL_LED_GREEN:
        led_handler = s_panel_green;
        break;
    default:
        return;
    }

    if (s_cur_state[led_beep] == mode)
    {
        return;
    }

    if (led_handler)
    {
        switch (mode)
        {
        case LED_NOTIFY_MODE_OFF:
            led_stop(led_handler);
            break;
        case LED_NOTIFY_MODE_ON:
            led_stop(led_handler);
            led_set_mode(led_handler, LOOP_PERMANENT, "300,");
            led_start(led_handler);
            break;
        case LED_NOTIFY_MODE_NORMAL_1:
            led_stop(led_handler);
            led_set_mode(led_handler, LOOP_PERMANENT, "300,14700,");
            led_start(led_handler);
            break;
        case LED_NOTIFY_MODE_NORMAL_2:
            led_stop(led_handler);
            led_set_mode(led_handler, LOOP_PERMANENT, "300,300,300,14100,");
            led_start(led_handler);
            break;
        case LED_NOTIFY_MODE_ALARM_1:
            led_stop(led_handler);
            led_set_mode(led_handler, LOOP_PERMANENT, "200,3800,");
            led_start(led_handler);
            break;
        case LED_NOTIFY_MODE_ALARM_2:
            led_stop(led_handler);
            led_set_mode(led_handler, LOOP_PERMANENT, "200,200,200,3400,");
            led_start(led_handler);
            break;
        case LED_NOTIFY_MODE_ALARM_NET_DOWN:        //网络异常，闪2下
            led_stop(led_handler);
            led_set_mode(led_handler, 1, "200,200,200，1000");
            led_set_blink_over_callback(led_handler, net_down_over_callback);
            led_start(led_handler);
            break;
        case LED_NOTIFY_MODE_ALARM_HY_STOP:
            led_stop(led_handler);
            led_set_mode(led_handler, LOOP_PERMANENT, "200,200,200,200,200,3000,");
            led_start(led_handler);
            break;
        case LED_NOTIFY_MODE_BOOT:
            led_stop(led_handler);
            led_set_mode(led_handler, 1, "200,200,200,200,200,200,200,200,");
            led_start(led_handler);
            break;
        }
    }

    s_cur_state[led_beep] = mode;
}

static void led_run(void *parameter)
{
    while (1)
    {
        led_ticks();
        rt_thread_mdelay(LED_TICK_TIME);
    }
}

static void switch_on(void* param)
{
    rt_base_t* pin = param;
    switch (*pin)
    {
    case SYS_LED :
        rt_pin_write(*pin, PIN_LOW);
        break;
    default:
        rt_pin_write(*pin, PIN_HIGH);
        break;
    }
}

static void switch_off(void* param)
{
    rt_base_t* pin = param;
    switch (*pin)
    {
    case SYS_LED :
        rt_pin_write(*pin, PIN_HIGH);
        break;
    default:
        rt_pin_write(*pin, PIN_LOW);
        break;
    }
}

static int led_setup(void)
{
    rt_pin_mode(SYS_LED, PIN_MODE_OUTPUT);
    s_sys_led_pin = rt_malloc(sizeof(rt_base_t));
    *s_sys_led_pin = SYS_LED;
    s_sys_led = led_create(switch_on, switch_off, s_sys_led_pin);
    led_set_mode(s_sys_led, LOOP_PERMANENT, "500,500,");
    led_start(s_sys_led);

    s_panel_yellow_pin = rt_malloc(sizeof(rt_base_t));
    *s_panel_yellow_pin = PANEL_YELLOW_PIN;
    s_panel_yellow = led_create(switch_on, switch_off, s_panel_yellow_pin);

    s_panel_green_pin = rt_malloc(sizeof(rt_base_t));
    *s_panel_green_pin = PANEL_GREEN_PIN;
    s_panel_green = led_create(switch_on, switch_off, s_panel_green_pin);

    s_panel_red_pin = rt_malloc(sizeof(rt_base_t));
    *s_panel_red_pin = PANEL_RED_PIN;
    s_panel_red = led_create(switch_on, switch_off, s_panel_red_pin);

    s_beep_pin = rt_malloc(sizeof(rt_base_t));
    *s_beep_pin = PANEL_BEEP_PIN;
    s_beep = led_create(switch_on, switch_off, s_beep_pin);

    rt_thread_t tid = RT_NULL;
    tid = rt_thread_create("signal_led", led_run,
    RT_NULL, 512,
    RT_THREAD_PRIORITY_MAX / 2, 100);
    if (tid != RT_NULL)
        rt_thread_startup(tid);


    led_beep(PANEL_BEE, LED_NOTIFY_MODE_BOOT);
    return RT_EOK;
}
INIT_APP_EXPORT(led_setup);
